﻿using IntelligentGateway.Model;
using IntelligentGateway.Services;
using IntelligentGateway.Tools;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace IntelligentGateway.Controllers.api
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserInfoController : ControllerBase
    {
        [HttpPost]
        [Route("Delete")]
        public ResponseMessage Delete(UserInfo t)
        {
            DaServices<UserInfo> services = new DaServices<UserInfo>();
            return services.Delete(t);
        }
        [HttpPost]
        [Route("Insert")]
        public ResponseMessage Insert(UserInfo t)
        {
            ResponseMessage response = new ResponseMessage();
            DaServices<UserInfo> services = new DaServices<UserInfo>();
            if(string.IsNullOrEmpty(t.Password))
            {
                response.errcode = 1;
                response.errmsg = "密码不能为空!";
                return response;
            }
            t.Password =  SecureTools.Encrypt(t.Password);
            return services.Insert(t);
        }
        [HttpPost]
        [Route("SelectAllByFilter")]
        public ResponseMessage SelectAllByFilter(UserInfo t, int startindex, int endindex)
        {
            DaServices<UserInfo> services = new DaServices<UserInfo>();
            return services.SelectAllByFilter(t, startindex, endindex);
        }
        [HttpPost]
        [Route("SelectAllData")]
        public ResponseMessage SelectAllData()
        {
            DaServices<UserInfo> services = new DaServices<UserInfo>();
            return services.SelectAllData();
        }
        [HttpPost]
        [Route("SelectByFilter")]
        public ResponseMessage SelectByFilter(UserInfo t)
        {
            DaServices<UserInfo> services = new DaServices<UserInfo>();
            return services.SelectByFilter(t);
        }
        [HttpPost]
        [Route("SelectByKey")]
        public ResponseMessage SelectByKey(int Key)
        {
            DaServices<UserInfo> services = new DaServices<UserInfo>();
            return services.SelectByKey(Key);
        }
        [HttpPost]
        [Route("Update")]
        public ResponseMessage Update(UserInfo t)
        {
            DaServices<UserInfo> services = new DaServices<UserInfo>();
            t.Password = null;
            return services.UpdateDemand(t);
        }
        [HttpPost]
        [Route("UpdatePassword")]
        public ResponseMessage UpdatePassword(string LoginCode,string OldPassword,string NewPassword)
        {
            UserServices services = new UserServices();      
            return services.UpdatePassword(LoginCode, OldPassword, NewPassword);
        }
        [HttpPost]
        [Route("ResetPassword")]
        public ResponseMessage ResetPassword(string LoginCode)
        {
            UserServices services = new UserServices();
            return services.ResetPassword(LoginCode);
        }
    }
}
